﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class manage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        ShuaXin();
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        using (DataClassesDataContext ds = new DataClassesDataContext())
        {
            gua p = new gua();
            p.xuhao = Convert.ToInt32(TextBox1.Text.Trim());
            p.guatu = TextBox2.Text.Trim();
            p.guaming = TextBox4.Text.Trim();
            p.guawen = TextBox3.Text;
            ds.gua.InsertOnSubmit(p);
            ds.SubmitChanges();

            ShuaXin();
            TextBox1.Text = "";
            TextBox2.Text = "";
            TextBox3.Text = "";
            TextBox4.Text = "";

        }
    }
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {

    }

    protected void ShuaXin()
    {
        using (DataClassesDataContext ds = new DataClassesDataContext())
        {
            GridView1.DataSource = from q in ds.gua
                                   orderby q.xuhao
                                   select q;
            GridView1.DataBind();
        }
    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        using (DataClassesDataContext ds = new DataClassesDataContext())
        {
            var rd = (from q in ds.gua
                     where q.id == Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value)
                     select q).FirstOrDefault();
            if (rd!=null)
            {
                ds.gua.DeleteOnSubmit(rd);
                ds.SubmitChanges();
            }
        }

        Response.Redirect("Manage.aspx");
    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;       

        ShuaXin();
    }
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        //using (DataClassesDataContext ds = new DataClassesDataContext())
        //{
        //    var r = (from q in ds.gua
        //             where q.id == int.Parse(GridView1.DataKeys[e.RowIndex].Values[0].ToString())
        //             select q).FirstOrDefault();
        //    if (r != null)
        //    {
        //        r.xuhao = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString();
        //        r.PaperState = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[4].Controls[0])).Text.ToString();
        //        r.AnswerTime = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[6].Controls[0])).Text.ToString();

        //        ds.SubmitChanges();

        //        GridView1.EditIndex = -1;
        //        ShuaXin ();
        //    }

        //}
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;

        ShuaXin();
    }
}